/*
 * This file is a part of qloudgen-esb. 
 * You can redistribute qloudgen-esb and/or modify it under the terms of the Lesser GNU General Public License version 3. 
 * qloudgen-esb is distributed WITHOUT ANY WARRANTY. 
 * 
 * See the Lesser GNU General Public License for more details at http://www.gnu.org/licenses/.
 *  
 * Copyright (C) 2014 Qloudgen Technology
*/

package com.qloudgen.csb.test;

public class ServerBase {
	protected int perfInterval;
	protected long count = 0;

    public ServerBase(int perfInterval) {
    	this.perfInterval = perfInterval*1000;
    }
    
	public static class Sum {
    	int nThreads;
    	long perfCount = 0;
    	long totalMps = 0;

		public Sum(int nThreads) {
			this.nThreads = nThreads;
		}
		
		public synchronized void sum(long mps) {
			totalMps = totalMps + mps;
			perfCount++;
			if (perfCount >= nThreads) {
				System.out.println("total mps: " + totalMps);
				totalMps = 0;
				perfCount = 0;
			}    	
		}
	}

    protected void perf(final Sum isum) {
    	new Thread(new Runnable() {
			@Override
			public void run() {	
				while (true) {
					long t = System.currentTimeMillis();
					try {
						Thread.sleep(perfInterval);
					} catch (InterruptedException e) {}
					long mps = -1;
					mps = count*1000
						/(System.currentTimeMillis() - t);
					count = 0;
					System.out.println("mps: " + mps);
					
					isum.sum(mps);
				}
			}    		    		
    	}).start();    	
    }
}

